<?php

namespace app\shop\controller\car;

use app\common\model\sms\Sms;
use app\shop\controller\Controller;
use app\shop\model\user\User as UserModel;
use app\common\model\car\Car as CarModel;
use app\shop\model\user\Grade;
use think\facade\Db;

/**
 * 车辆管理
 */
class Car extends Controller
{
    /**
     * 列表
     */
    public function index($car_number = '', $car_type = null,$status='')
    {

        $where = $where2 = array();
        if (!empty($car_number)) {
            $where[] = ['a.car_number','=',$car_number];
        }
        if (!empty($car_type)) {

            $where[] = ['a.car_type','=',$car_type];
        }
        if (!empty($status)) {
            $where[] = ['a.status','=',$status-1];

        }
        $where[] = ['a.is_delete','=',0];
        $list  = Db::name('car')
                ->alias('a')
                ->field('a.*,b.type_name,u.mobile')
                ->join('jjjshop_car_type b','a.car_type = b.type_id','LEFT')
                ->join('jjjshop_user u','a.user_id=u.user_id','left')
                ->where($where)
                ->order('a.create_time desc')
                ->paginate($this->postData(),false);
        $grade = Db::name('car_type')->where($where2)->order('type_id asc')->select();
        return $this->renderSuccess('', compact('list', 'grade'));
    }


    /**
     * 删除
     */
    public function delete()
    {
        // 详情
        $car_id = $this->request->param('car_id');
        $model = new CarModel;
        $res = $model->where(array('car_id'=>$car_id))->update(array('is_delete'=>1));

        if ($res) {
            return $this->renderSuccess('删除成功');
        }
        return $this->renderError($model->getError() ?: '删除失败');
    }

    public function detail()
    {
        $id = $this->request->param('id');

        $model = new CarModel;

        $info = $model->detail($id);
        $brand = Db::name('car_brand')->select();

        $grade = Db::name('car_type')->order('type_id asc')->select();
        return $this->renderSuccess('设置成功',compact('info','grade','brand'));
    }

    public function editCar()
    {
        $data = $this->request->param();
        $info = $data['info'];
//        if(isset($data['status'])){
//            $info['status'] = $data['status'];
//        }
       unset($info['create_time']);
        unset($info['update_time']);
        $model = new CarModel;
        $res = $model->where(array('car_id'=>$data['id']))->update($info);
        if ($res) {
            return $this->renderSuccess('操作成功');
        }
        return $this->renderError($model->getError() ?: '操作失败');
    }

}
